package com.steed.rocketmq.consumer.config;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.steed.rocketmq.consumer.consumer.DemoConsumer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Properties;

/**
 * ONS 配置类
 *
 * @author za-zhubin002
 * Created in 14:23 2017/10/27
 */
@Configuration
@EnableConfigurationProperties(OnsConsumerProperties.class)
public class OnsConsumerConfiguration {
    @Resource
    private DemoConsumer demoConsumer;

    @Bean(name = "testConsumer")
    public Consumer consumer(Properties properties){
        Consumer consumer = ONSFactory.createConsumer(properties);
        consumer.subscribe("T-WOW-ACCOUNT-171026", "*", demoConsumer);
        consumer.start();
        return consumer;
    }

    @Bean
    public Properties properties(OnsConsumerProperties onsConsumerProperties){
        Properties properties = new Properties();
        // MQ控制台创建的Producer ID
        properties.put(PropertyKeyConst.ConsumerId, onsConsumerProperties.getConsumer_id());
        // 鉴权用AccessKey，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, onsConsumerProperties.getAccess_key());
        // 鉴权用SecretKey，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, onsConsumerProperties.getSecret_key());
        // 设置 TCP 接入域名
        properties.put(PropertyKeyConst.ONSAddr, onsConsumerProperties.getOns_addr());
        return properties;
    }
}
